﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using OpenQuarters.WebQuarters.MVCAreas;
using OpenQuarters.EntityBase;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Core.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.CreateArea("sitemap.xml", "OpenQuarters.WebQuarters.Areas.CMS.Controllers",
                routes.MapRoute(null, "sitemap.xml", new { controller = "Sitemap", action = "Index" })
            );

            routes.CreateArea("site", "OpenQuarters.WebQuarters.Areas.Site.Controllers",
                routes.MapRoute(null, "site/{*id}", new { controller = "Home", action = "Index" })
            );

            routes.CreateArea("cms", "OpenQuarters.WebQuarters.Areas.CMS.Controllers",
                routes.MapRoute(null, "cms/{controller}/{action}/{*id}", new { controller = "Home", action = "Index", id = "" })
            );

            routes.CreateArea("cms", "OpenQuarters.WebQuarters.Areas.CMS.Controllers",
                routes.MapRoute(null, "cms/{controller}/{*id}", new { controller = "Home", action = "Index" })
            );
        }

        public virtual void RegisterCustomRoutes(RouteCollection routes)
        {
        }

        protected void Application_Start()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new AreaViewEngine());
            RegisterRoutes(RouteTable.Routes);

            this.GetType().GetMethod("RegisterCustomRoutes").Invoke(this.GetType().GetNewObject(), new object[] { RouteTable.Routes });
            JavaScript.RegisterCommonScripts();
        }

        public virtual void Application_BeginRequest()
        {
        }
       

        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

            //Ensure SessionID in order to prevent the folloing exception
            //when the Application Pool Recycles
            //[HttpException]: Session state has created a session id, but cannot
            //				   save it because the response was already flushed by </span>
            string sessionId = Session.SessionID;
        }
    }
}
